﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKMaterialStatistic;
using YJKRevitView;

namespace YJKWndrStatisticsExcel.AreaTable.DataManager
{
	public class FireCompartmentAreaInfo
	{
        public FireCompartmentAreaInfo(Autodesk.Revit.DB.Document doc, ref FlexCell.Grid grid, GridSetCommonInfo gridSetInfo)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			this.StatisticData();
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private void StatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			try
			{
				foreach (ViewPlan viewPlan in FloorSelectManager.GetAllFireAreaViewPlan(this.m_doc))
				{
					this.m_lstAreas.Clear();
					List<Area> areasInViewPlan = this.GetAreasInViewPlan(this.m_doc, viewPlan);
					this.m_lstAreas.AddRange(areasInViewPlan);
					if (this.m_gridSetInfo.LinkFile)
					{
						foreach (RevitLinkInstance revitLinkInstance in this.GetAllRevitLinkInstancesInDoc(viewPlan))
						{
							Document linkDocument = revitLinkInstance.GetLinkDocument();
							List<Area> areasInViewPlan2 = this.GetAreasInViewPlan(linkDocument, viewPlan);
							if (areasInViewPlan2.Count > 0)
							{
								this.m_lstAreas.AddRange(areasInViewPlan2);
							}
						}
					}
					if (this.m_lstAreas.Count > 0)
					{
						List<List<string>> list = new List<List<string>>();
						foreach (Area area in this.m_lstAreas)
						{
							string text = "";
							Parameter parameter = area.GetParameter("区域编号");
							if (parameter != null)
							{
								text = parameter.AsString();
							}
							double num = area.Area;
							num = UnitConvert.AreaConverFromAPI(this.m_doc, num);
							int num2 = this.IsSameNumber(text, list);
							if (num2 == -1)
							{
								List<string> list2 = new List<string>();
								list2.Add(text);
								parameter = area.GetParameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
								if (parameter != null)
								{
									list2.Add(parameter.AsString());
								}
								else
								{
									list2.Add("");
								}
								list2.Add(num.ToString("0.00"));
								list.Add(list2);
							}
							else
							{
								double num3 = Convert.ToDouble(list[num2][2]);
								list[num2][2] = (num3 + num).ToString("0.00");
							}
						}
						this.m_dicData.Add(viewPlan.Name, list);
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private int IsSameNumber(string strNumber, List<List<string>> lstLstItem)
		{
			for (int i = 0; i < lstLstItem.Count; i++)
			{
				if (lstLstItem[i][0] == strNumber)
				{
					return i;
				}
			}
			return -1;
		}

		private List<Area> GetAreasInViewPlan(Autodesk.Revit.DB.Document doc, ViewPlan vp)
		{
			List<Area> list = new List<Area>();
			try
			{
				foreach (Element element in new FilteredElementCollector(doc, vp.Id).OfClass(typeof(SpatialElement)))
				{
					Area area = element as Area;
					if (area != null && AreaInfoManager.AreaIsFireArea(area))
					{
						list.Add(area);
					}
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		private List<RevitLinkInstance> GetAllRevitLinkInstancesInDoc(ViewPlan view)
		{
			IEnumerable<ElementId> enumerable = new FilteredElementCollector(this.m_doc, view.Id).OfClass(typeof(RevitLinkInstance)).ToElementIds();
			List<RevitLinkInstance> list = new List<RevitLinkInstance>();
			foreach (ElementId elementId in enumerable)
			{
				Element element = this.m_doc.GetElement(elementId);
				if (!(element.GetType() != typeof(RevitLinkInstance)))
				{
					RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
					if (revitLinkInstance != null)
					{
						list.Add(revitLinkInstance);
					}
				}
			}
			return list;
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSetInfo.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num = 3;
			int num2 = 0;
			foreach (KeyValuePair<string, List<List<string>>> keyValuePair in this.m_dicData)
			{
				List<List<string>> value = keyValuePair.Value;
				num2 += value.Count;
			}
			if (num2 == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + num2 + 1;
			Dictionary<string, List<List<string>>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int firstRow = num + 1;
			while (enumerator.MoveNext())
			{
				KeyValuePair<string, List<List<string>>> keyValuePair2 = enumerator.Current;
				List<List<string>> value2 = keyValuePair2.Value;
				if (value2.Count > 0)
				{
					for (int i = 0; i < value2.Count; i++)
					{
						num++;
						this.m_grid.Cell(num, i + 1).Text = keyValuePair2.Key;
						for (int j = 0; j < value2[i].Count; j++)
						{
							this.m_grid.Cell(num, j + 2).Text = value2[i][j];
							this.m_grid.Cell(num, j + 2).Alignment = AlignmentEnum.CenterCenter;
						}
					}
					this.m_grid.Range(firstRow, 1, num, 1).Merge();
					this.m_grid.Range(firstRow, 2, num, this.m_grid.Cols - 1).Sort(2, SortTypeEnum.Text, SortEnum.Ascending);
					firstRow = num + 1;
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			if (!this.m_gridSetInfo.SetColWidth)
			{
				for (int l = 0; l < this.m_grid.Cols; l++)
				{
					this.m_grid.Column(l).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

        private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSetInfo;

		private List<Area> m_lstAreas = new List<Area>();

		private Dictionary<string, List<List<string>>> m_dicData = new Dictionary<string, List<List<string>>>();

		private class AreaComparer : IComparer<Area>
		{
			public int Compare(Area x, Area y)
			{
				return y.Area.CompareTo(x.Area);
			}
		}
	}
}
